package com.pig4cloud.pig.module.service.electric.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.common.core.exception.TransactionalException;
import com.pig4cloud.pig.common.core.util.OrderUtil;
import com.pig4cloud.pig.module.api.entity.Parameter;
import com.pig4cloud.pig.module.api.entity.electric.ElectricMeter;
import com.pig4cloud.pig.module.api.entity.electric.ElectricOrder;
import com.pig4cloud.pig.module.api.entity.platform.PlatformPay;
import com.pig4cloud.pig.module.mapper.electric.ElectricMeterMapper;
import com.pig4cloud.pig.module.mapper.electric.ElectricOrderMapper;
import com.pig4cloud.pig.module.service.base.IParameterService;
import com.pig4cloud.pig.module.service.electric.IElectricMeterService;
import com.pig4cloud.pig.module.service.electric.IElectricOrderService;
import com.pig4cloud.pig.module.service.platform.IPlatformPayService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;


/**
 * 电充值订单Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-02
 */
@Service
public class ElectricOrderServiceImpl extends ServiceImpl<ElectricOrderMapper, ElectricOrder> implements IElectricOrderService {

	@Resource
	private ElectricOrderMapper orderMapper;
	@Resource
	private ElectricMeterMapper electricMeterMapper;
	@Resource
	private IElectricMeterService electricMeterService;
	@Resource
	private IPlatformPayService platformPayService;
	@Resource
	private IParameterService parameterService;

	@Override
	@Transactional
	public int addOrder(ElectricMeter meter) {
		ElectricOrder order = new ElectricOrder();
		int ret = 0;
		try{
			double money = meter.getRechargeMoney();
			PlatformPay platformPay = platformPayService.getAccountByEstateId(meter.getEstateId());
			if(platformPay == null){
				platformPay = platformPayService.getDefaultAccount();
			}

			Parameter parameter = parameterService.getParameterById(1);

			//添加 订单表数据
			String orderNo = platformPay.getSysCode() + OrderUtil.getOrderNoByTimes();
			order.setOrderNo(orderNo);
			order.setOutTradeNo(orderNo);
			order.setDeviceId(meter.getId());
			order.setEstateId(meter.getEstateId());
			order.setEstateName(meter.getEstateName());
			order.setDeviceName(meter.getDeviceName());
			order.setDeviceSn(meter.getDeviceSn());
			order.setUserId(0);
//			order.setNickName(user.getNickName());
			order.setCreateTime(new Date());
			order.setConcentratorId(meter.getConcentratorId());
			order.setConcentratorSn(meter.getConcentratorSn());
			order.setStatus(0);
			order.setRefundStatus(0);
			order.setPayType(2);//线下付款

			order.setBuyChargerCapacity(new BigDecimal(money));
			order.setPayPrice(new BigDecimal(money));
			order.setTotalPrice(new BigDecimal(money));

			order.setPayPlatformId(0);
			if(platformPay!=null) {
				if(parameter.getElectricPayPlatform() == 1){
					order.setPayPlatformId(platformPay.getId());
				}
			}

			ret = orderMapper.insert(order);
			if(ret <= 0){
				throw new TransactionalException("orderMapper insert error");
			}
			//开始充值
			ret = electricMeterService.rechargeElectricAsync(meter,order.getId(),money);
			if(ret <= 0){
				throw new TransactionalException("electricMeterService rechargeElectricAsync error");
			}

		}catch (Exception e){
			e.printStackTrace();
			throw new TransactionalException("充电 addOrder system error");
		}
		return order.getId();
	}
}
